﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace Luminji.Core.Safe
{
    public class RijndaelHelper
    {
        int _bufferSize = 128 * 1024;
        /// <summary>
        /// 缓冲区大小，默认128 * 1024
        /// </summary>
        public int BufferSize
        {
            get { return _bufferSize; }
            set { _bufferSize = value; }
        }

        byte[] _salt = { 134, 216, 7, 36, 88, 164, 91, 227, 174, 76, 191, 197, 192, 154, 200, 248 };
        /// <summary>
        /// 密钥salt，默认{ 134, 216, 7, 36, 88, 164, 91, 227, 174, 76, 191, 197, 192, 154, 200, 248 }
        /// </summary>
        public byte[] Salt
        {
            get { return _salt; }
            set { _salt = value; }
        }

        byte[] _iv = { 134, 216, 7, 36, 88, 164, 91, 227, 174, 76, 191, 197, 192, 154, 200, 248 };
        /// <summary>
        /// 初始化向量，默认{ 134, 216, 7, 36, 88, 164, 91, 227, 174, 76, 191, 197, 192, 154, 200, 248 }
        /// </summary>
        public byte[] IV
        {
            get { return _iv; }
            set { _iv = value; }
        }

        public string EncryptString(string input, string key)
        {
            RijndaelManaged rijndaelProvider = new RijndaelManaged();
            using (SymmetricAlgorithm algorithm = CreateRijndael(key, Salt))
            {
                rijndaelProvider.Key = algorithm.Key;
                rijndaelProvider.IV = IV;
                ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor();

                byte[] inputData = Encoding.Unicode.GetBytes(input);
                byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length);
                return Encoding.Unicode.GetString(encryptedData);
            }
        }
        public string DecryptString(string input, string key)
        {
            RijndaelManaged rijndaelProvider = new RijndaelManaged();
            using (SymmetricAlgorithm algorithm = CreateRijndael(key, Salt))
            {
                rijndaelProvider.Key = algorithm.Key;
                rijndaelProvider.IV = IV;
                ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor();
                byte[] inputData = Encoding.Unicode.GetBytes(input);
                byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length);
                return Encoding.Unicode.GetString(decryptedData);
            }
        }

        //初始化并返回对称加密算法
        protected SymmetricAlgorithm CreateRijndael(string password, byte[] salt)
        {
            PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, salt, "SHA256", 1000);
            SymmetricAlgorithm sma = Rijndael.Create();
            sma.KeySize = 256;
            sma.Key = pdb.GetBytes(32);
            sma.Padding = PaddingMode.PKCS7;
            return sma;
        }
    }
}
